/**
 * Generate code from /{{projectName}}-core/src/main/java/{{packageName}}/{{apis}}/rest/Abstract{{apiEntities}}Resource.java.hbs
 */
package cn.ibizlab.api.rest;

import java.util.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.util.ObjectUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.ApiOperation;
import cn.ibizlab.util.domain.ImportResult;
import cn.ibizlab.api.dto.*;
import cn.ibizlab.api.mapping.*;
import cn.ibizlab.core.open.domain.OpenAccessAuth;
import cn.ibizlab.core.open.service.OpenAccessAuthService;
import cn.ibizlab.core.open.filter.OpenAccessAuthSearchContext;
import cn.ibizlab.util.annotation.VersionCheck;

/**
 * 实体[OpenAccessAuth] rest实现
 *
 * @author generator
 */
@Slf4j
public abstract class AbstractOpenAccessAuthResource {

    @Autowired
    public OpenAccessAuthService openAccessAuthService;

    @Autowired
    @Lazy
    public OpenAccessAuthDTOMapping openAccessAuthDtoMapping;

    @Autowired
    @Lazy
    public OpenAccessAuthFilterDTOMapping openAccessAuthFilterDtoMapping;


    /**
    * 创建Create 接入认证绑定
    * 
    *
    * @param dto dto
    * @return ResponseEntity<OpenAccessAuthDTO>
    */
    @ApiOperation(value = "创建Create", tags = {"接入认证绑定" },  notes = "OpenAccessAuth-Create ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccessAuth-Create-all') or hasPermission(this.openAccessAuthDtoMapping.toDomain(#dto),'uaa-OpenAccessAuth-Create')")
    @PostMapping(value = {"open_access_auths","open_access_auths"})
    public ResponseEntity<OpenAccessAuthDTO> create
            (@Validated @RequestBody OpenAccessAuthDTO dto) {
        OpenAccessAuth domain = openAccessAuthDtoMapping.toDomain(dto);
        openAccessAuthService.create(domain);
        OpenAccessAuth rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(openAccessAuthDtoMapping.toDto(rt));
    }

    /**
    * 获取Get 接入认证绑定
    * 
    *
    * @param id id
    * @return ResponseEntity<OpenAccessAuthDTO>
    */
    @ApiOperation(value = "获取Get", tags = {"接入认证绑定" },  notes = "OpenAccessAuth-Get ")
    @PostAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccessAuth-Get-all') or hasPermission(this.openAccessAuthDtoMapping.toDomain(returnObject.body),'uaa-OpenAccessAuth-Get')")
    @GetMapping("open_access_auths/{id}")
    public ResponseEntity<OpenAccessAuthDTO> getById
            (@PathVariable("id") String id) {
        OpenAccessAuth rt = openAccessAuthService.get(id);
        return ResponseEntity.status(HttpStatus.OK).body(openAccessAuthDtoMapping.toDto(rt));
    }

    /**
    * 删除Remove 接入认证绑定
    * 
    *
    * @param id id
    * @return ResponseEntity<Boolean>
    */
    @ApiOperation(value = "删除Remove", tags = {"接入认证绑定" },  notes = "OpenAccessAuth-Remove ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccessAuth-Remove-all') or hasPermission(this.openAccessAuthService.get(#id),'uaa-OpenAccessAuth-Remove')")
    @DeleteMapping("open_access_auths/{id}")
    public ResponseEntity<Boolean> removeById
            (@PathVariable("id") String id) {
        Boolean rt = openAccessAuthService.remove(id);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 更新Update 接入认证绑定
    * 
    *
    * @param id id
    * @param dto dto
    * @return ResponseEntity<OpenAccessAuthDTO>
    */
    @ApiOperation(value = "更新Update", tags = {"接入认证绑定" },  notes = "OpenAccessAuth-Update ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccessAuth-Update-all') or hasPermission(this.openAccessAuthService.get(#id),'uaa-OpenAccessAuth-Update')")
    @VersionCheck(entity = "openaccessauth" , versionfield = "updateTime")
    @PutMapping("open_access_auths/{id}")
    public ResponseEntity<OpenAccessAuthDTO> updateById
            (@PathVariable("id") String id, @Validated @RequestBody OpenAccessAuthDTO dto) {
        OpenAccessAuth domain = openAccessAuthDtoMapping.toDomain(dto);
        domain.setId(id);
        openAccessAuthService.update(domain);
        OpenAccessAuth rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(openAccessAuthDtoMapping.toDto(rt));
    }

    /**
    * 校验CheckKey 接入认证绑定
    * 
    *
    * @param dto dto
    * @return ResponseEntity<Integer>
    */
    @ApiOperation(value = "校验CheckKey", tags = {"接入认证绑定" },  notes = "OpenAccessAuth-CheckKey ")
    @PostMapping("open_access_auths/checkkey")
    public ResponseEntity<Integer> checkKey
            (@Validated @RequestBody OpenAccessAuthDTO dto) {
        OpenAccessAuth domain = openAccessAuthDtoMapping.toDomain(dto);
        Integer rt = openAccessAuthService.checkKey(domain);
        return ResponseEntity.status(HttpStatus.OK).body(rt);
    }

    /**
    * 草稿GetDraft 接入认证绑定
    * 
    *
    * @param dto dto
    * @return ResponseEntity<OpenAccessAuthDTO>
    */
    @ApiOperation(value = "草稿GetDraft", tags = {"接入认证绑定" },  notes = "OpenAccessAuth-GetDraft ")
    @GetMapping("open_access_auths/getdraft")
    public ResponseEntity<OpenAccessAuthDTO> getDraft
            (@SpringQueryMap OpenAccessAuthDTO dto) {
        OpenAccessAuth domain = openAccessAuthDtoMapping.toDomain(dto);
        OpenAccessAuth rt = openAccessAuthService.getDraft(domain);
        return ResponseEntity.status(HttpStatus.OK).body(openAccessAuthDtoMapping.toDto(rt));
    }

    /**
    * 保存Save 接入认证绑定
    * 
    *
    * @param dto dto
    * @return ResponseEntity<OpenAccessAuthDTO>
    */
    @ApiOperation(value = "保存Save", tags = {"接入认证绑定" },  notes = "OpenAccessAuth-Save ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccessAuth-Save-all') or hasPermission(this.openAccessAuthDtoMapping.toDomain(#dto),'uaa-OpenAccessAuth-Save')")
    @PostMapping("open_access_auths/save")
    public ResponseEntity<OpenAccessAuthDTO> save
            (@Validated @RequestBody OpenAccessAuthDTO dto) {
        OpenAccessAuth domain = openAccessAuthDtoMapping.toDomain(dto);
        openAccessAuthService.save(domain);
        OpenAccessAuth rt = domain;
        return ResponseEntity.status(HttpStatus.OK).body(openAccessAuthDtoMapping.toDto(rt));
    }

    /**
    * 查询FetchCompatible 接入认证绑定
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<OpenAccessAuthDTO>>
    */
    @ApiOperation(value = "查询FetchCompatible", tags = {"接入认证绑定" },  notes = "OpenAccessAuth-FetchCompatible ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccessAuth-FetchCompatible-all') or hasPermission(#dto,'uaa-OpenAccessAuth-FetchCompatible')")
    @PostMapping("open_access_auths/fetchcompatible")
    public ResponseEntity<List<OpenAccessAuthDTO>> fetchCompatible
            (@Validated @RequestBody OpenAccessAuthFilterDTO dto) {
        OpenAccessAuthSearchContext context = openAccessAuthFilterDtoMapping.toDomain(dto);
        Page<OpenAccessAuth> domains = openAccessAuthService.searchCompatible(context) ;
        List<OpenAccessAuthDTO> list = openAccessAuthDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }

    /**
    * 查询FetchDefault 接入认证绑定
    * 
    *
    * @param dto dto
    * @return ResponseEntity<List<OpenAccessAuthDTO>>
    */
    @ApiOperation(value = "查询FetchDefault", tags = {"接入认证绑定" },  notes = "OpenAccessAuth-FetchDefault ")
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccessAuth-FetchDefault-all') or hasPermission(#dto,'uaa-OpenAccessAuth-FetchDefault')")
    @PostMapping("open_access_auths/fetchdefault")
    public ResponseEntity<List<OpenAccessAuthDTO>> fetchDefault
            (@Validated @RequestBody OpenAccessAuthFilterDTO dto) {
        OpenAccessAuthSearchContext context = openAccessAuthFilterDtoMapping.toDomain(dto);
        Page<OpenAccessAuth> domains = openAccessAuthService.searchDefault(context) ;
        List<OpenAccessAuthDTO> list = openAccessAuthDtoMapping.toDto(domains.getContent());
            return ResponseEntity.status(HttpStatus.OK)
            .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
            .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
            .header("x-total", String.valueOf(domains.getTotalElements()))
            .body(list);
    }


    /**
    * 批量新建接入认证绑定
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccessAuth-Create-all')")
    @ApiOperation(value = "批量新建接入认证绑定", tags = {"接入认证绑定" },  notes = "批量新建接入认证绑定")
	@PostMapping("open_access_auths/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<OpenAccessAuthDTO> dtos) {
        openAccessAuthService.createBatch(openAccessAuthDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量删除接入认证绑定
    * @param ids ids
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccessAuth-Remove-all')")
    @ApiOperation(value = "批量删除接入认证绑定", tags = {"接入认证绑定" },  notes = "批量删除接入认证绑定")
	@DeleteMapping("open_access_auths/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<String> ids) {
        openAccessAuthService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量更新接入认证绑定
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccessAuth-Update-all')")
    @ApiOperation(value = "批量更新接入认证绑定", tags = {"接入认证绑定" },  notes = "批量更新接入认证绑定")
	@PutMapping("open_access_auths/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<OpenAccessAuthDTO> dtos) {
        openAccessAuthService.updateBatch(openAccessAuthDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量保存接入认证绑定
    * @param dtos
    * @return ResponseEntity<Boolean>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccessAuth-Save-all')")
    @ApiOperation(value = "批量保存接入认证绑定", tags = {"接入认证绑定" },  notes = "批量保存接入认证绑定")
	@PostMapping("open_access_auths/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<OpenAccessAuthDTO> dtos) {
        openAccessAuthService.saveBatch(openAccessAuthDtoMapping.toDomain(dtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    /**
    * 批量导入接入认证绑定
    * @param config 导入模式
    * @param ignoreError 导入中忽略错误
    * @return ResponseEntity<ImportResult>
    */
    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','uaa-OpenAccessAuth-Save-all')")
    @ApiOperation(value = "批量导入接入认证绑定", tags = {"接入认证绑定" },  notes = "批量导入接入认证绑定")
	@PostMapping("open_access_auths/import")
    public ResponseEntity<ImportResult> importData(@RequestParam(value = "config" , required = false) String config ,@RequestParam(value = "ignoreerror", required = false, defaultValue = "true") Boolean ignoreError ,@RequestBody List<OpenAccessAuthDTO> dtos) {
        return  ResponseEntity.status(HttpStatus.OK).body(openAccessAuthService.importData(config,ignoreError,openAccessAuthDtoMapping.toDomain(dtos)));
    }

}
